package cn.neusoft.servlet;

import cn.neusoft.bean.Business;
import cn.neusoft.bean.Food;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.query.FoodQuery;
import cn.neusoft.service.IFoodService;
import cn.neusoft.service.impl.FoodService;
import cn.neusoft.utils.IOUtils;
import cn.neusoft.utils.JsonUtils;
import cn.neusoft.vo.ResultBean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/food")
public class FoodServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        IFoodService foodService=new FoodService();
        String what=req.getParameter("what");


        ResultBean resultBean=null;

        try {
            if ("save".equals(what)) {
                System.out.println("添加或修改餐品");
                //读取输入流中的json字符串(请求体的内容)
                String jsonStr = IOUtils.readStr(req.getInputStream());
                //将json字符串转成Business对象
                Food food = JsonUtils.jsonStrToObj(jsonStr,Food.class);
                //判断business中有没有主键
                if(food.getFoodId()==null){
                    resultBean = foodService.add(food);
                }else{
                    resultBean = foodService.update(food);
                }
            }else if ("del".equals(what)) {
                //http://localhost:8080/business?what=del&id=1001
                System.out.println("删除餐品");
                String id = req.getParameter("id");
                resultBean = foodService.delete(Integer.parseInt(id));
            } else if ("getById".equals(what)) {
                System.out.println("通过id查询商品");
                String id = req.getParameter("id");
                resultBean = foodService.getById(Integer.parseInt(id));
            } else if ("page".equals(what)) {
                System.out.println("分页搜索商品");
                /*
                  请求体---->BusinessQuery
                  {
                    pageNow: 1,
                    pageCount: 5,
                    businessName: "小吃",
                    businessAddress: "新乡"
                  }
                 */
                String jsonStr = IOUtils.readStr(req.getInputStream());
                FoodQuery foodQuery = JsonUtils.jsonStrToObj(jsonStr, FoodQuery.class);
                resultBean = foodService.getByPage(foodQuery);

            }
            else if("batchDel".equals(what)){
                String jsonStr=IOUtils.readStr(req.getInputStream());
                JSONArray jsonArray=JSONObject.parseArray(jsonStr);
                List<Integer> ids=jsonArray.toJavaList(Integer.class);
                System.out.println("json转集合"+ids);
                resultBean=foodService.batchDel(ids);

            }
            else {
                System.out.println("无服务");
                resultBean = ResultBean.ERROR("no service");
            }

        }catch (Exception e){
            e.printStackTrace();
            resultBean=ResultBean.ERROR(e.getMessage());
        }
        String resultJsonStr = JSONObject.toJSONString(resultBean);
        resp.getWriter().write(resultJsonStr);



    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
doGet(req,resp);
    }
}
